<?php
/**
 * 抽奖设置
 * User: admin
 * Date: 2018/3/29
 * Time: 15:01
 */

namespace Admin\Controller;


class LotterydrawController extends AdminController
{

    /**
     * 动物列表
     */
    public function animalList(){
        $list = $this->lists('animal', '','id asc');
        $this->assign('_list', $list);
        $this->meta_title = '动物设置';
        $this->display();
    }

    /**
     * 动物设置
     */
    public function animalEdit(){
        $id = I('id');
        $animal = D('animal')->where('id='.$id)->find();
        if(IS_POST) {
            $data['day_total'] = I('day_total');
            $data['night_total'] = I('night_total');
            $data['uptime'] =time();
            $result = M('animal')->where('id='.$id)->save($data);
            if($result){
                $this->success('修改成功！',U('animalList'));
            }else{
                $this->error('修改失败！');
            }
        }
        $this->assign ( 'info', $animal);
        $this->display ();
    }

    /**
     * 抽奖规则
     */
    public function rule(){
        $list = M('luck_rule')->select();
        foreach ($list as &$li){
            if($li['status'] ==1){
                $li['status_txt'] = '<span class="quantity label label-success">启用</span>';
            }else {
                $li['status_txt'] = '<span class="quantity label label-error">禁用</span>';
            }
        }
        $this->assign ( '_list', $list);
        $this->display ();
    }

    /**
     * 改变抽奖规则
     */
    public function changeStatus(){
        $id = I('id');
        $method = I('method');
        if($method == 'resume'){
            $map['status']=0;
            $where['id'] = array('not in','3');
            M('luck_rule')->where($where)->save($map);
            $data['status'] = 1;
            $data['uptime'] = time();
            M('luck_rule')->where('id='.$id)->save($data);
            if($id==2){
                $rec_data['action'] ="启用规则二";
                $rec_data['uptime'] = time();
                M('rule_record')->add($rec_data);
            }
        }else{
            $map['status'] =0;
            M('luck_rule')->save($map);
        }
        $this->success('操作成功');
    }

    /**
     * 开龙记录
     */
    public function rule_record(){
        $list = M('rule_record')->order('id desc')->select();
        $this->assign ( '_list', $list);
        $this->display ();
    }

    /**
     *档位列表
     */
    public function chance_rule(){
        $list = M('chance_rule')->order('id asc')->select();
        foreach ($list as &$li){
            if($li['status'] ==1){
                $li['status_txt'] = '<span class="quantity label label-success">启用</span>';
            }else {
                $li['status_txt'] = '<span class="quantity label label-error">禁用</span>';
            }
        }
        $this->assign ( '_list', $list);
        $this->display ();
    }

    /**
     * 编辑档位
     */
    public function edit_chance_rule(){
        if(IS_POST){
            $post =I('');
            $total = $post['val1']+$post['val2']+$post['val3']+$post['val4']+$post['val5'];
            if($total>100){
                $this->error('概率相加不能超过100！');
            }
            $id = $post['id'];
            $data['val1']=$post['val1'];
            $data['val2']=$post['val2'];
            $data['val3']=$post['val3'];
            $data['val4']=$post['val4'];
            $data['val5']=$post['val5'];
            $data['uptime'] = time();
            $result = M('chance_rule')->where('id='.$id)->save($data);
            if($result){
                $this->success('修改成功！',U('chance_rule'));
            }else{
                $this->error('修改失败！');
            }
        }
        $id = I('id');
        $info = M('chance_rule')->where('id='.$id)->find();
        $this->assign ( 'info', $info);
        $this->display ();
    }

    /**
     * 启用/禁用档位
     */
    public  function change_chance_rule(){
        $id = I('id');
        $method = I('method');
        if($method == 'resume'){
            $map['status']=0;
            $where['id'] = array('not in','0');
            M('chance_rule')->where($where)->save($map);
            $data['status'] = 1;
            $data['uptime'] = time();
            M('chance_rule')->where('id='.$id)->save($data);

        }else{
            $map['status'] =0;
            M('chance_rule')->save($map);
        }
        $this->success('操作成功');
    }



}